/**
 *  Product: Posterita Web-Based POS, Webstore and Adempiere Plugin
 *  Copyright (C) 2007  Posterita Ltd
 *  This file is part of POSterita
 *  
 *  POSterita is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 */

package org.compiere.db;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.Savepoint;
import java.sql.Statement;
import java.util.Map;

import org.compiere.util.DB;
import org.compiere.util.Trx;

/**
 * Connection that is used to execute query on Server for Client processes
 * Need for Jasper Report processes as the Jasper Manager uses a connection
 *  
 * @author Ashley G Ramdass 
 */
public class ServerConnection implements Connection
{
	
	private String trxName = null;

	public ServerConnection() {		
	}
	
	public ServerConnection(String trxName) {
		this.trxName = trxName;
	}
	
	public void clearWarnings() throws SQLException
	{
		throw new java.lang.UnsupportedOperationException ("Method clearWarnings() not yet implemented.");
	}

	public void close() throws SQLException
	{
		if (trxName != null) {
	    	Trx trx = Trx.get(trxName, false);
	    	if (trx != null)
	    		trx.close();
	    	trxName = null;
	    }
	}

	public void commit() throws SQLException
	{
	    if (trxName != null) {
	    	Trx trx = Trx.get(trxName, false);
	    	if (trx != null)
	    		trx.commit(true);
	    }
	}

	public Statement createStatement() throws SQLException
	{
		return DB.createStatement();
	}

	public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException
	{
		return DB.createStatement(resultSetType, resultSetConcurrency, trxName);
	}

	public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException
	{
		return createStatement(resultSetType, resultSetConcurrency);
	}

	public boolean getAutoCommit() throws SQLException
	{
	    return (trxName != null);
	}

	public String getCatalog() throws SQLException
	{
	    throw new java.lang.UnsupportedOperationException ("Method getCatalog() not yet implemented.");
	}

	public int getHoldability() throws SQLException
	{
	    throw new java.lang.UnsupportedOperationException ("Method getHoldability() not yet implemented.");
	}

	public DatabaseMetaData getMetaData() throws SQLException
	{
	    throw new java.lang.UnsupportedOperationException ("Method getMetaData() not yet implemented.");
	}

	public int getTransactionIsolation() throws SQLException
	{
	    return Connection.TRANSACTION_READ_COMMITTED;
	}

	public Map<String, Class<?>> getTypeMap() throws SQLException
	{
	    throw new java.lang.UnsupportedOperationException ("Method getTypeMap() not yet implemented.");
	}

	public SQLWarning getWarnings() throws SQLException
	{
	    return null;
	}

	public boolean isClosed() throws SQLException
	{
	    return false;
	}

	public boolean isReadOnly() throws SQLException
	{
	    return false;
	}

	public String nativeSQL(String sql) throws SQLException
	{
	    throw new java.lang.UnsupportedOperationException ("Method nativeSQL() not yet implemented.");
	}

	public CallableStatement prepareCall(String sql) throws SQLException
	{
		return DB.prepareCall(sql);
	}

	public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException
	{
		return DB.prepareCall(sql, resultSetConcurrency, trxName);
	}

	public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException
	{
		return DB.prepareCall(sql, resultSetConcurrency, trxName);
	}

	public PreparedStatement prepareStatement(String sql) throws SQLException
	{
		return DB.prepareStatement(sql);
	}

	public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException
	{
	    throw new java.lang.UnsupportedOperationException ("Method prepareStatement() not yet implemented.");
	}

	public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws SQLException
	{
	    throw new java.lang.UnsupportedOperationException ("Method prepareStatement() not yet implemented.");
	}

	public PreparedStatement prepareStatement(String sql, String[] columnNames) throws SQLException
	{
	    throw new java.lang.UnsupportedOperationException ("Method prepareStatement() not yet implemented.");
	}

	public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) throws SQLException
	{
		return DB.prepareStatement(sql, resultSetConcurrency, resultSetConcurrency);
	}

	public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException
	{
		return prepareStatement(sql, resultSetType, resultSetConcurrency);
	}

	public void releaseSavepoint(Savepoint savepoint) throws SQLException
	{
	    throw new java.lang.UnsupportedOperationException ("Method releaseSavepoint() not yet implemented.");
	}

	public void rollback() throws SQLException
	{
	    if (trxName != null) {
	    	Trx trx = Trx.get(trxName, false);
	    	if (trx != null)
	    		trx.rollback(true);
	    }
	}

	public void rollback(Savepoint savepoint) throws SQLException
	{
	    throw new java.lang.UnsupportedOperationException ("Method rollback() not yet implemented.");
	}

	public void setAutoCommit(boolean autoCommit) throws SQLException
	{
	    if (autoCommit) {
	    	if (trxName != null) {
	    		Trx trx = Trx.get(trxName, false);
	    		if (trx != null)
	    			trx.close();
	    	}
	    } else {
	    	if (trxName == null) {
	    		trxName = Trx.createTrxName();
	    	}
	    }
	}

	public void setCatalog(String catalog) throws SQLException
	{
	    throw new java.lang.UnsupportedOperationException ("Method setCatalog() not yet implemented.");
	}

	public void setHoldability(int holdability) throws SQLException
	{
	    throw new java.lang.UnsupportedOperationException ("Method setHoldability() not yet implemented.");
	}

	public void setReadOnly(boolean readOnly) throws SQLException
	{
	}

	public Savepoint setSavepoint() throws SQLException
	{
    	return setSavepoint(null);
	}

	public Savepoint setSavepoint(String name) throws SQLException
	{
		if (trxName == null) {
	    	trxName = Trx.createTrxName();
	    }
	    Trx trx = Trx.get(trxName, false);
    	return trx.setSavepoint(name);
	}

	public void setTransactionIsolation(int level) throws SQLException
	{	    
	}

	public void setTypeMap(Map<String, Class<?>> arg0) throws SQLException
	{
	    throw new java.lang.UnsupportedOperationException ("Method setTypeMap() not yet implemented.");
	}

	/* Java 6 support - teo_sarca BF [ 1806700 ] *
	public java.sql.Array createArrayOf(String typeName, Object[] elements) throws SQLException {
	    throw new java.lang.UnsupportedOperationException ("Method not yet implemented.");
	}

	public java.sql.Blob createBlob() throws SQLException {
	    throw new java.lang.UnsupportedOperationException ("Method not yet implemented.");
	}

	public java.sql.Clob createClob() throws SQLException {
	    throw new java.lang.UnsupportedOperationException ("Method not yet implemented.");
	}

	public java.sql.NClob createNClob() throws SQLException {
	    throw new java.lang.UnsupportedOperationException ("Method not yet implemented.");
	}

	public java.sql.SQLXML createSQLXML() throws SQLException {
	    throw new java.lang.UnsupportedOperationException ("Method not yet implemented.");
	}

	public java.sql.Struct createStruct(String typeName, Object[] attributes) throws SQLException {
	    throw new java.lang.UnsupportedOperationException ("Method not yet implemented.");
	}

	public java.util.Properties getClientInfo() throws SQLException {
	    throw new java.lang.UnsupportedOperationException ("Method not yet implemented.");
	}

	public String getClientInfo(String name) throws SQLException {
	    throw new java.lang.UnsupportedOperationException ("Method not yet implemented.");
	}

	public boolean isValid(int timeout) throws SQLException {
	    throw new java.lang.UnsupportedOperationException ("Method not yet implemented.");
	}

	public void setClientInfo(java.util.Properties properties) throws java.sql.SQLClientInfoException {
	}

	public void setClientInfo(String name, String value) throws java.sql.SQLClientInfoException {
	}

	public boolean isWrapperFor(Class<?> iface) throws SQLException {
	    throw new java.lang.UnsupportedOperationException ("Method not yet implemented.");
	}

	public <T> T unwrap(Class<T> iface) throws SQLException {
	    throw new java.lang.UnsupportedOperationException ("Method not yet implemented.");
	}
	/* Java 6 support - teo_sarca BF [ 1806700 ] */
}
